//
//  DSLoginViewModel.m
//  DSJY
//
//  Created by 冯华强 on 16/10/8.
//  Copyright © 2016年 dsjy. All rights reserved.
//  登录VM

#import "DSLoginViewModel.h"
#import "DSAccount.h"
#import "DSAccountViewModel.h"

@interface DSLoginViewModel ()
//游客登录请求命令
@property (nonatomic, strong, readwrite) RACCommand *visitorsLoginCommand;
@end

@implementation DSLoginViewModel

#pragma mark - 初始化
- (instancetype)init
{
    if (self = [super init]) {
        self.account = [[DSAccount alloc] init];
        //初始化
        [self initialization];
    }
    return self;
}

- (void)initialization
{
    
    @weakify(self);
    //处理登录时的信号
    _loginEndSignal = [RACSignal combineLatest:@[RACObserve(self.account, loginid) , RACObserve(self.account, loginPassword)] reduce:^id(NSString *loginid, NSString *loginPassword){
        
        return @(loginid.length && loginPassword.length);
    }];
    
    [self createCmdSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        [DSNetworkTool loginWithParams:self.account success:^(NSDictionary *responseObject) {
            //返回值
            self.response = [YQResponse mj_objectWithKeyValues:responseObject];
            [self veriResponse:self.response finishedCallBack:^{
                DSAccount *account = [DSAccount mj_objectWithKeyValues:[responseObject[@"attrs"] objectForKey:@"member"]];
                MTLog(@"%@", account);
                [subscriber sendNext:account];
            }];
            [subscriber sendCompleted];
        } failure:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:@"系统异常!"];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    
    //设置提示
    [self executingWithCommand:self.requestCommand];
    
    // 游客登录逻辑
    _visitorsLoginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [SVProgressHUD show];
            [DSNetworkTool visitorsLoginWithSuccess:^(NSDictionary *responseObject) {
                [SVProgressHUD dismiss];
                //返回值
                self.response = [YQResponse mj_objectWithKeyValues:responseObject];
                [self veriResponse:self.response finishedCallBack:^{
                    DSAccount *account = [DSAccount mj_objectWithKeyValues:[responseObject[@"attrs"] objectForKey:@"member"]];
                    //写入用户信息
                    account.visitors = YES;  //游客
                    [DSAccountViewModel sharedAccount].account = account;
                    [[DSAccountViewModel sharedAccount] write];
                    
                    //发送登录成功的通知
                    [MTNotificationCenter postNotificationName:DSLoginRegisgerSuccessNote object:nil];
                }];
                [subscriber sendCompleted];
            } failure:^(NSError *error) {
                [SVProgressHUD dismiss];
                [SVProgressHUD showErrorWithStatus:@"系统异常!"];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
    }];
}

@end
